public class Application {
	private int type;
	public int generated_data_size = 0;

	
	private int nextTime=0;
	public Application(int type) {
		super();
		this.type = type;
	}

	public DataUnit generateData(int t) {
		DataUnit du = new DataUnit(new byte[P_rand(type)], -1);
		generated_data_size += du.data.length;
		return du;
	}

	public DataUnit generateData(int deadline,int t) {
		//如果到达了产生数据的时隙，那么产生数据单元，并且更新下一个时隙时间
		if(t==nextTime)
		{
			DataUnit du = new DataUnit(new byte[5*(int)(Math.random()*100)], -1);
			generated_data_size += du.data.length;
			du.bound=deadline;
			nextTime+=(int)(e_d(1.0/deadline));
			return du;
		}
		else
		{
			return null;
		}

	}
	public int P_rand(int Lamda) { // 泊松分布
		int x = 0;
		double c = Math.exp(-Lamda), u, b = 1;
		do {
			u = Math.random();
			b *= u;
			if (b >= c)
				x++;
		} while (b >= c);
		return x;
	}
	
	
	/**
	 * 泊松过程的模拟
	 * 时间间隔满足指数分布
	 * @param lambda
	 * @return
	 */
	static double e_d(double lambda)
	{
		float z;
		do
		{
			z = (float) Math.random();
			//printf("z is %f\n", 1-z);
		}while(z == 0 || z == 1);
		return (-(1/lambda)*Math.log(1-z));
	}
	
	public int getType()
	{
		return type;
	}
	
}
